import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import 'package:syncfusion_flutter_core/theme.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
final textStyle = TextStyle(color: Color.fromRGBO(11,123,118, 1.0),decoration: TextDecoration.none,fontSize: 18.sp);
class DataGtid extends StatefulWidget{

  final List<StockQuant>? dataList;

  final String? title;

  final int? scrollBars;

  DataGtid({Key? key,required this.dataList,required this.title,required this.scrollBars}):super(key: key);

  @override
  _DataGtidState createState() => _DataGtidState();
}
class _DataGtidState extends State<DataGtid>{
  // List<Employee> _employees = getEmployeeData();
  late DataSource _dataSource;
  final DataGridController _controller = DataGridController();
  final ScrollController _verticalScrollController = ScrollController();
  late Timer timer;
  double _currentRow = 0;


  @override
  Widget build(BuildContext context) {
    return Card(
      // margin:  const EdgeInsets.all(24.0),
        clipBehavior: Clip.antiAlias,
        elevation: 1.0,
        color: Colors.transparent,
        child: _buildDataGrid());

  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _dataSource = DataSource(widget.dataList!);
    timer = Timer.periodic(Duration(seconds: 3), (Timer t) => autoScroll());
  }

  @override
  void dispose() {
    timer.cancel();
    super.dispose();
  }

  autoScroll(){
    if (_verticalScrollController.position.pixels ==
        _verticalScrollController.position.maxScrollExtent) {
      _currentRow = 0;
    }else{
      _currentRow += widget.scrollBars!;
    }
    _controller.scrollToCell(_currentRow, 1,canAnimate:true);
  }

  _buildDataGrid(){
    return  Column(
      children: [
        Container(
          // width: width,
          color: Color.fromRGBO(48,48,85, 1.0),
          child: Padding(
            padding: EdgeInsets.all(10),
            child: Row(
              children: [
                Text(widget.title!,style: TextStyle(color: Color.fromRGBO(255, 255, 255, 1),decoration: TextDecoration.none,fontSize: 25.sp),),
              ],
            ),
          ),
        ),
        Expanded(
          // width: width,
          // height: 200.h,
          child: SfDataGridTheme(
              data: SfDataGridThemeData(
                gridLineColor: Color.fromRGBO(46,47,83, 1.0),
                headerColor: Colors.transparent,
                headerHoverColor: Colors.transparent,
              ),
              child:SfDataGrid(
                controller: _controller,
                verticalScrollController: _verticalScrollController,
                columnWidthMode: ColumnWidthMode.fill,
                source: _dataSource,
                rowHeight: 55.h,
                headerRowHeight: 55.h,
                gridLinesVisibility: GridLinesVisibility.both,
                headerGridLinesVisibility: GridLinesVisibility.both,
                columns: [
                  GridColumn(
                      columnName: 'no',
                      maximumWidth: 100.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '序号',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  // GridColumn(
                  //     columnName: 'code',
                  //     label: Container(
                  //         padding: EdgeInsets.symmetric(horizontal: 16.0),
                  //         alignment: Alignment.center,
                  //         child: Text(
                  //           '物料编码',
                  //           style: textStyle,
                  //           overflow: TextOverflow.ellipsis,
                  //         ))),
                  GridColumn(
                      columnName: 'name',
                      // minimumWidth: 270.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '物料名称',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'spec',
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '规格型号',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  // GridColumn(
                  //     columnName: 'location',
                  //     minimumWidth: 350.r,
                  //     label: Container(
                  //         padding: EdgeInsets.symmetric(horizontal: 16.0),
                  //         alignment: Alignment.center,
                  //         child: Text(
                  //           '库房位置',
                  //           style: textStyle,
                  //           overflow: TextOverflow.ellipsis,
                  //         ))),
                  GridColumn(
                      columnName: 'quantity',
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '当前库存数量',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'proportion',
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '库存占比',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                ],
              )
          ),
        ),

      ],
    );
  }
}

Widget buildSoftwareProficiency(dynamic value) {
  Widget getLinearProgressBar(num progressValue) {
    return Container(
      width: 120.r,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
              height: 22.r,
              width: 120.r,
              child: ClipRRect(
                // 边界半径（`borderRadius`）属性，圆角的边界半径。
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
                child: LinearProgressIndicator(
                  value: progressValue / 100,
                  valueColor: AlwaysStoppedAnimation<Color>(progressValue < 50 ? Colors.red : Colors.green),
                  backgroundColor:progressValue < 50 ? Colors.red[100] : Colors.green[100],
                )
              ),),
          Text(' ' + (progressValue.toString() + '%'),style: TextStyle(color: Color.fromRGBO(255, 255, 255, 1),decoration: TextDecoration.none,fontSize: 18.sp),
          ),
        ],
      ),
    );
  }

  return getLinearProgressBar(value*100);
}
// class Employee {
//   Employee(this.id, this.code, this.name, this.model, this.storage, this.stock, this.ratio, this.alarm);
//   final String id;
//   final String code;
//   final String name;
//   final String model;
//   final String storage;
//   final String stock;
//   final String ratio;
//   final num alarm;
//
// }

// List<Employee> getEmployeeData() {
//   return [
//     Employee('1', '3010100156','35度中国劲牌(十金酒)盖','33.6*66.2mm','1列1排','431000','21.40%',21.40),
//     Employee('2', '1023403421','精致C40金色盖','41.7*51mm','2列3排','254900','12.60%',12.60),
//     Employee('3', '42101015690','贵州特曲T10茅台型盖','32.9*53.5mm','3列1排','515600','25.60%',25.60),
//     Employee('4', '20200980203','45度毛铺苦荞酒盖','32.3*59.3mm','4列2排','335800','16.70%',16.70),
//     Employee('5', '302020610406','茅台醇玖贰黑钻茅型盖','30.8*40mm','5列3排','114000','5.60%',5.60),
//     Employee('6', '22104245890','6.7mm双耳环拉二维码盖','63.6*6.7mm','6列1排','1238700','6.10%',6.10),
//     Employee('7', '3010100156','125ml35度中国进就盖(赋码)','26*40mm','7列2排','234000','11.60%',11.60),
//   ];
// }

class DataSource extends DataGridSource {
  DataSource(List<StockQuant> stockQuantList) {
    var index = 1;
    dataGridRows = stockQuantList
        .map<DataGridRow>((stockQuant) => DataGridRow(cells: [
      DataGridCell<int>(columnName: 'no', value: index++),
      // DataGridCell<String>(columnName: 'code', value: stockQuant.productCode),
      DataGridCell<String>(columnName: 'name', value: stockQuant.productName),
      DataGridCell<String>(columnName: 'spec', value: stockQuant.productDes),
      // DataGridCell<String>(columnName: 'location', value: stockQuant.locationName),
      DataGridCell<num>(columnName: 'quantity', value: stockQuant.quantity),
      DataGridCell<num>(columnName: 'proportion', value: stockQuant.proportion),
    ]))
        .toList();
  }

  List<DataGridRow> dataGridRows = [];

  @override
  List<DataGridRow> get rows => dataGridRows;

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        color: Colors.transparent,
        cells: row.getCells().map<Widget>((dataGridCell) {
          return dataGridCell.columnName == 'proportion'?buildSoftwareProficiency(dataGridCell.value):Container(
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 16.0),
              child: Text(
                dataGridCell.value.toString(),
                style: TextStyle(
                  fontSize: 18.sp,
                  color: Colors.white,
                  decoration: TextDecoration.none
                ),
                overflow: TextOverflow.ellipsis,
              ));
        }).toList());
  }
}
